package com.mh.aidldemo;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private EditText mEt1;
    private EditText mEt2;
    private TextView mTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEt1 = (EditText) findViewById(R.id.et_num1);
        mEt2 = (EditText) findViewById(R.id.et_num2);
        mTv = (TextView) findViewById(R.id.tv);

        Intent intentService = new Intent(this,AdditionService.class);
        bindService(intentService, mConnection, Context.BIND_AUTO_CREATE);
    }

    private IMyAidlInterface mService;
    ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = IMyAidlInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    public void add(View view) {
        try {
            addNum();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }


    public void addNum() throws RemoteException {

        //int x = Integer.parseInt(mEt1.getText().toString());
       // int y = Integer.parseInt(mEt2.getText().toString());
     //   int result = mService.add(x, y);
        mService.addPerson(new Person(8));

        mTv.setText(mService.getPersonList().get(0).age +"");
    }

}
